package com.xiake.utils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.ServletInputStream;

import net.sf.json.JSONArray;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class ClassUtil {
    public static Map<String, Object> toMap(String[] keys, Object... values) {
        Map<String, Object> params = new HashMap<String, Object>();
        if (keys.length == values.length) {

            for (int i = 0; i < keys.length; i++) {
                String key = keys[i];
                Object value = values[i];
                params.put(key, value);
            }
        } else if (keys.length == 1) {
            params.put(keys[0], values);
        } else {
            try {
                throw new Exception("key和value的个数不一致，无法转成map格式");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return params;
    }

    public static String requestToJson(ServletInputStream inStream) {
        ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
        String result = null;
        try {
            byte[] buff = new byte[8192]; // buff用于存放循环读取的临时数据
            int rc = 0;
            while ((rc = inStream.read(buff, 0, 1024)) > 0) {
                swapStream.write(buff, 0, rc);
            }
            byte[] in_b = swapStream.toByteArray(); // in_b为转换之后的结果?
            String payload = new String(in_b);
            System.out.println(payload);
            result = payload.substring(payload.indexOf(":") + 1, payload.length() - 1);
            if (result.startsWith("{")) {
                result = "[" + result + "]";
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                swapStream.flush();
                swapStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        return result;
    }

    public static String obj2Json(Object obj) throws JsonProcessingException {
        ObjectMapper omapper = new ObjectMapper();
        String json = omapper.writeValueAsString(obj);
        omapper = null;
        return json;
    }

    @SuppressWarnings({ "unchecked", "rawtypes" })
    public static Object jsonStrToObject(String json, Class clazz) {
        ObjectMapper omapper = new ObjectMapper();
        Object obj = null;
        try {
            obj = omapper.readValue(json, clazz);
        } catch (JsonParseException e) {
            e.printStackTrace();
        } catch (JsonMappingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        omapper = null;
        return obj;

    }

    @SuppressWarnings({ "deprecation", "rawtypes" })
    public static Object jsonStrToList(String json, Class clazz) {
        JSONArray jsonArray2 = JSONArray.fromObject(json);
        // 将josn数组转换成对应类的泛型集合
        Object obj = JSONArray.toList(jsonArray2, clazz);
        return obj;
    }

    public static String getStatusStr(int status) {
        String statusStr = status == 1 ? "待审核" : (status == 2 ? "审核通过" : (status == 3 ? "审核未通过" : (status == 4 ? "众筹失败"
                : (status == 5 ? "众筹成功" : (status == 6 ? "活动成功结束" : "")))));
        return statusStr;
    }

    public static long minsToSec(String mins) {
        long movieMin = 0;
        if (mins != null && !mins.trim().equals("")) {
            try {
                movieMin = Integer.valueOf(mins) + 2;
            } catch (Exception e) {
                String regEx = "[^0-9]";
                Pattern p = Pattern.compile(regEx);
                Matcher m = p.matcher(mins);
                movieMin = Integer.valueOf(m.replaceAll("").trim()) + 2;
                if (movieMin > 300) {
                    movieMin = 122;
                }
                regEx = null;
                p = null;
                m = null;
            }

        }
        return movieMin * 60 * 1000;
    }

    public static int strToInt(String str) {
        int intValue = 0;
        if (str != null && !str.trim().equals("")) {
            try {
                intValue = Integer.valueOf(str);
            } catch (Exception e) {
                String regEx = "[^0-9]";
                Pattern p = Pattern.compile(regEx);
                Matcher m = p.matcher(str);
                intValue = Integer.valueOf(m.replaceAll("").trim());
                regEx = null;
                p = null;
                m = null;
            }

        }
        return intValue;
    }
}
